package me.stone.stanimeclient.core.ui.item

import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.drawable.Drawable
import android.text.style.ImageSpan

class CenteredImageSpan(drawable: Drawable) : ImageSpan(drawable) {

    override fun draw(
        canvas: Canvas, text: CharSequence?,
        start: Int, end: Int, x: Float, top: Int, y: Int, bottom: Int,
        paint: Paint
    ) {
        val metrics = paint.fontMetricsInt
        val transY = (metrics.descent + metrics.ascent - drawable.bounds.bottom) / 2f + y - 1f
        canvas.save()
        canvas.translate(x, transY)
        drawable.draw(canvas)
        canvas.restore()
    }

}